package com.fbee.smartcommunities.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbee.smartcommunities.config.LocalDateConverter;
import com.fbee.smartcommunities.entity.Notice;
import com.fbee.smartcommunities.mapper.NoticeMapper;
import com.fbee.smartcommunities.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.aspectj.weaver.ast.Not;
import org.springframework.format.datetime.joda.LocalDateTimeParser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-20
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Resource
    private NoticeMapper noticeMapper;

    @Override
    public Page<Notice> getPage(Integer pageNo, Integer pageSize, String keyword) {
        Page<Notice> page = new Page<>(pageNo, pageSize);
        if(keyword == null){
            return noticeMapper.selectDistinctNoticesByTitle(page);
        } else {
            QueryWrapper<Notice> noticeQueryWrapper = new QueryWrapper<>();
            noticeQueryWrapper.like("title",keyword);
            return noticeMapper.selectDistinctNoticesByTitleLike(keyword,page);
        }
    }

    @Override
    public Boolean saveNotice(String content, String title, Long[] userIds) {
        if(userIds != null){
            for (Long userId : userIds) {
                this.save(new Notice(title,content, LocalDateTime.now(),userId,false,false));
            }
        }
        return true;
    }

    @Override
    public Boolean update(Long id, String content, String title) {
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("title",title);
        updateWrapper.set("content",content);
        return this.update(updateWrapper);
    }

    @Override
    public List<Notice> getByUid(Long uid) {
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        List<Notice> list = this.list(queryWrapper.eq("state", 1)
                .and(wrapper -> wrapper.eq("recevice_uid", uid).or().eq("recevice_uid", 0)));
        return list;
    }


}
